import requests
from lxml import etree
session = requests.Session()
headers = {
    'authority': 'www.python-spider.com',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'max-age=0',
    'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'document',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'none',
    'sec-fetch-user': '?1',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    'Referer': 'https://www.python-spider.com/challenge/34',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
}

cookies = {
    "sessionid": "wkuzkfozxtqgmpdbpc10xarc4noyp42x",
}
response = session.get('https://www.python-spider.com/challenge/34', headers=headers, cookies=cookies)
cookie_dict = requests.utils.dict_from_cookiejar(response.cookies)
cookie_params = 'sessionid={}; yuanrenxue34={}'.format(cookie_dict['sessionid'], cookie_dict['yuanrenxue34'])
eval_text = etree.HTML(response.text).xpath('//script/text()')[0]

import execjs
import re
with open("34.js", "r", encoding="utf-8") as f:
    result = execjs.compile(f.read()).call("decode", cookie_params, eval_text)
    cookies['iloveu'] = re.search("iloveu=(.*?);", result).group(1)

response = session.get('https://www.python-spider.com/challenge/34', headers=headers, cookies=cookies)
print(response.text)



# 需要考虑的点：
# 1.  session保持
# 2.  我们要拿到 document.cookie  也就是返回的 yuanrenxue34 ，送到程序里
# 3.  我们要拿到页面中的 rind 和 rnns 值
